package me.shiqiang.im.utils;

import com.aliyun.openservices.ons.api.Action;
import com.tst.common.exception.MicroServiceException;
import com.tst.common.returnobj.ReturnObjCode;
import me.shiqiang.im.enums.MessageCType;
import me.shiqiang.im.enums.MessageMType;
import me.shiqiang.im.microservice.AppNotificationMicroService;
import me.shiqiang.im.microservice.returnobj.NotifyFinReturnObj;
import me.shiqiang.im.msg.MsgBody;
import me.shiqiang.im.vo.AppNotificationVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by John on 2018/3/20
 */
@Component
public class AppNotifyUtil {

    @Autowired
    private AppNotificationMicroService appNotificationMicroService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    public Action appNotify(AppNotificationVo notificationVo) {
        try {
            NotifyFinReturnObj returnObj = appNotificationMicroService.appNotification(notificationVo);
            if (!ReturnObjCode.SUCCESS.getCode().equals(returnObj.getCode())) {
                logger.warn("APP通知微服务服务返回失败,response:{}", returnObj.toString());
                throw new MicroServiceException(returnObj.getMsg());
            }
            return Action.CommitMessage;
        } catch (Exception e) {
            logger.error(String.format("消息通知失败,msg:%s", e));
            return Action.ReconsumeLater;
        }
    }

    public void translateMtype(MsgBody msgBody, AppNotificationVo notificationVo) {
        if (msgBody.getcType() == Integer.parseInt(MessageCType.GROUP)) {
            if (Integer.parseInt(MessageMType.TEXT) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": " + msgBody.getMsg());
            } else if (Integer.parseInt(MessageMType.IMG) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [图片]");
            } else if (Integer.parseInt(MessageMType.AUDIO) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [语音]");
            } else if (Integer.parseInt(MessageMType.VEDIO) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [视频]");
            } else if (Integer.parseInt(MessageMType.NOTICE) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [通知]");
            } else if (Integer.parseInt(MessageMType.FILE) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [文件]");
            }else if (Integer.parseInt(MessageMType.EMOJI) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getFromName() + ": [表情]");
            } else {
                notificationVo.setBody(msgBody.getFromName() + ": [未知的消息类型]");
            }
        }else {
            if (Integer.parseInt(MessageMType.TEXT) == msgBody.getmType()) {
                notificationVo.setBody(msgBody.getMsg());
            } else if (Integer.parseInt(MessageMType.IMG) == msgBody.getmType()) {
                notificationVo.setBody("[图片]");
            } else if (Integer.parseInt(MessageMType.AUDIO) == msgBody.getmType()) {
                notificationVo.setBody("[语音]");
            } else if (Integer.parseInt(MessageMType.VEDIO) == msgBody.getmType()) {
                notificationVo.setBody("[视频]");
            }else if (Integer.parseInt(MessageMType.NOTICE) == msgBody.getmType()) {
                notificationVo.setBody("[通知]");
            }else if (Integer.parseInt(MessageMType.FILE) == msgBody.getmType()) {
                notificationVo.setBody("[文件]");
            }else if (Integer.parseInt(MessageMType.EMOJI) == msgBody.getmType()) {
                notificationVo.setBody("[表情]");
            } else {
                notificationVo.setBody("[未知的消息类型]");
            }
        }
    }

}
